﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SisCRA.Business;
using SisCRA.Classes;
using SisCRA.Reports;

namespace SisCRA.Forms
{
    public partial class FormClosingGrades : Form
    {
        #region -Constructor-

        /// <summary>
        /// Construtor padrão.
        /// </summary>
        public FormClosingGrades()
        {
            try
            {
                InitializeComponent();
                LoadComboBoxSearchDiscipline();
                CleanComboBoxSearchDiscipline();
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao buscar dados.\r\nPor favor, verifique suas configurações de conexão com o banco de dados.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region -Events-

        private void comboBoxSearchDiscipline_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadReport();
            }
            catch
            {

            }
        }

        #endregion

        #region -Private Methods-

        /// <summary>
        /// Método que carrega o comboBoxSearchDiscipline.
        /// </summary>
        private void LoadComboBoxSearchDiscipline()
        {
            try
            {
                DisciplineBLL disciplineBLL = new DisciplineBLL();
                this.comboBoxSearchDiscipline.DataSource = disciplineBLL.GetAllDiscipline();
                this.comboBoxSearchDiscipline.ValueMember = "ID";
                this.comboBoxSearchDiscipline.DisplayMember = "CODIGO";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que limpa o comboBoxSearchDiscipline.
        /// </summary>
        private void CleanComboBoxSearchDiscipline()
        {
            try
            {
                this.comboBoxSearchDiscipline.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que carrega o relatório de fechamento de disciplina.
        /// </summary>
        private void LoadReport()
        {
            try
            {
                StudentEnrollment studentEnrollment = new StudentEnrollment();
                studentEnrollment.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                StudentEnrollmentBLL studentEnrollmentBLL = new StudentEnrollmentBLL();

                ReportDataSet reportDS = new ReportDataSet();
                reportDS.Tables.Remove("V-ALUNO-DISCIPLINA");
                reportDS.Tables.Add(studentEnrollmentBLL.GetEnrollmentByDiscipline(studentEnrollment));

                ClosingGradesCrystalReport report = new ClosingGradesCrystalReport();
                report.SetDataSource(reportDS);

                this.crystalReportViewer.ReportSource = report;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
